package com.ssm.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.ssm.subject.application.convert.SubjectAnswerDtoConvert;
import com.ssm.subject.application.convert.SubjectInfoDTOConvert;
import com.ssm.subject.application.dto.SubjectInfoDTO;
import com.ssm.subject.common.entity.PageInfo;
import com.ssm.subject.common.entity.PageResult;
import com.ssm.subject.common.entity.Result;
import com.ssm.subject.domain.bo.SubjectAnswerBO;
import com.ssm.subject.domain.bo.SubjectInfoBO;
import com.ssm.subject.domain.service.SubjectInfoDomainService;
import com.ssm.subject.infra.basic.entity.SubjectInfoEs;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/subject")
@Log4j2
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;
    @Resource
    private RocketMQTemplate rocketMQTemplate;
    /**
     * 新增题目 (题目只关联小分类id 和 标签 id)
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("SubjectController.add.subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectParse(), "题目解析不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()), "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.subjectInfoDtoToBo(subjectInfoDTO);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.info("SubjectController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询题目列表
     * @param
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.subjectInfoDTO{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.subjectInfoDtoToBo(subjectInfoDTO);
            //直接返回BO，不转DTO 繁琐
            PageResult<SubjectInfoBO> subjectPage = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(subjectPage);
        } catch (Exception e) {
            log.info("SubjectController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目列表失败");
        }
    }

    /**
     * 查询题目详情
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.subjectInfoDtoToBo(subjectInfoDTO);
            SubjectInfoBO subjectInfobo = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO subjectInfoDTO1 = SubjectInfoDTOConvert.INSTANCE.subjectInfoBoTODto(subjectInfobo);
            return Result.ok(subjectInfoDTO1);
        } catch (Exception e) {
            log.info("SubjectController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

    /**
     * 关键词搜索
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> querySubjectByKeyword(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(subjectInfoDTO.getKeyWord()), "关键词不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.subjectInfoDtoToBo(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoEs> boPageResult = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.info("SubjectController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }

    /**
     * 获取题目贡献榜
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoBO> subjectInfoBOList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> subjectInfoDTOList = SubjectInfoDTOConvert.INSTANCE.subjectInfosToDtos(subjectInfoBOList);
            return Result.ok(subjectInfoDTOList);
        } catch (Exception e) {
            log.info("SubjectController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取题目贡献榜失败");
        }
    }

    /**
     * 测试MQ发送消息
     * @param id
     * @return
     */
    @RequestMapping("/pushMessage")
    public Result<Boolean> get(@RequestParam("id") int id) {
        rocketMQTemplate.convertAndSend("first-topic","你好,Java旅途" + id);
        return Result.ok(true);
    }

}
